package net.zjitc.action.Admin.Product;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.zjitc.damain.Product.Product;
import net.zjitc.service.CategorySecond.CategorySecondService;
import net.zjitc.service.Product.ProductService;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by 95729 on 2017/3/21.
 */
@Controller
@Scope("prototype")
public class ProductAction extends ActionSupport implements ModelDriven<Product> {
    @Resource
    private ProductService productService;
    @Resource
    private CategorySecondService categorySecondService;

    private File img;
    private String imgFileName;

    public File getImg() {
        return img;
    }

    public void setImg(File img) {
        this.img = img;
    }

    public String getImgFileName() {
        return imgFileName;
    }

    public void setImgFileName(String imgFileName) {
        this.imgFileName = imgFileName;
    }

    private Product Model = new Product();

    public void setModel(Product model) {
        Model = model;
    }

    @Override
    public Product getModel() {
        return Model;
    }


    public String findAll(){
        List<Product> productlist = productService.findAll();
        ActionContext.getContext().put("productlist",productlist);
        return "findAll";
    }

    public String Delete(){
        Product product = productService.findById(Model.getPid());
        productService.Delete(product);
        return "Delete";
    }

    public String addUI(){
        List cslist = categorySecondService.findAll();
        ActionContext.getContext().put("cslist",cslist);
        return "addUI";
    }

    public String add(){
        Model.setPdate(new Date());
        String realPath = ServletActionContext.getServletContext().getRealPath("/products/4/");
        if (img != null){
            File newFile = new File(new File(realPath),imgFileName);
            try {
                FileUtils.copyFile(img,newFile);
                Model.setImage("/products/4/"+imgFileName);
                productService.save(Model);
            } catch (IOException e) {
                e.printStackTrace();
                return "tolist";
            }
        }
        return "add";
    }

    public String editUI(){
        Product product = productService.findById(Model.getPid());
        Model.setPname(product.getPname());
        Model.setPdesc(product.getPdesc());
        Model.setMarketPrice(product.getMarketPrice());
        Model.setShopPrice(product.getShopPrice());
        Model.setIsHot(product.getIsHot());
        Model.setPdate(product.getPdate());
        List cslist = categorySecondService.findAll();
        ActionContext.getContext().put("cslist",cslist);
        return "editUI";
    }

    public String edit(){
        return "edit";
    }







}
